#include "taskstyleeditor.h"
#include "ui_taskstyleeditor.h"
#include <QColorDialog>
#include <QInputDialog>
#include <QDir>
#include <QSettings>

#include "taskmanager.h"

TaskStyleEditor::TaskStyleEditor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TaskStyleEditor)
{
    ui->setupUi(this);

    QVector<StyleItem *> vctStyle = TaskManager::getInstance()->allStyles();
    ui->tableWidget->setRowCount(vctStyle.count());
    for(int i=0; i<vctStyle.count(); ++i){
        StyleItem* pItem = vctStyle.at(i);
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(pItem->getType()));

        QColor clr = QColor(pItem->getBrushColor());

        QTableWidgetItem* pTableItem = new QTableWidgetItem();
        pTableItem->setData(Qt::DisplayRole,QString("%1-%2-%3").arg(clr.red()).arg(clr.green()).arg(clr.blue()));
        pTableItem->setData(Qt::BackgroundColorRole,clr);
        ui->tableWidget->setItem(i,1,pTableItem);
    }

    // 觉得添加和删除按钮意义不大，在此隐藏
    ui->pushButton_AddStyle->setHidden(true);
    ui->pushButton_RemoveStyle->setHidden(true);
}

TaskStyleEditor::~TaskStyleEditor()
{
    delete ui;
}

void TaskStyleEditor::on_pushButton_EditStyle_clicked()
{
    int curRow = ui->tableWidget->currentRow();
    if(curRow < 0 || curRow>ui->tableWidget->rowCount()-1)
        return;

    QTableWidgetItem* pCurItem = ui->tableWidget->item(curRow,0);
    QRgb rgb = QColorDialog::getRgba();
    if(rgb == 0xffffffff)
    {
        return;
    }

    QString sTaskType = pCurItem->text();

    StyleItem * pStyle = TaskManager::getInstance()->getStyle(sTaskType);
    if(NULL == pStyle)
    {
        pStyle = TaskManager::getInstance()->addStyle(sTaskType);
    }
    pStyle->setBrushColor(rgb);

    ui->tableWidget->item(curRow,1)->setData(Qt::BackgroundColorRole,QColor(rgb));

    // 通知界面，修改使用了这个样式的绘图
    TaskManager::getInstance()->updateStyle(sTaskType);

    // 修改setting文件，确保下次使用时是更新后的
    QDir settingDir(QDir::currentPath());
    settingDir.cdUp();
    settingDir.setPath(settingDir.absolutePath().append("/config/FY4TaskOptimization"));
    if(!settingDir.exists())
    {
        settingDir.mkdir(settingDir.absolutePath());
    }

    QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
    settings.beginGroup("TaskStyle");
    settings.setValue(sTaskType,rgb);
    settings.endGroup();
}

void TaskStyleEditor::on_pushButton_AddStyle_clicked()
{
    QString sName = QInputDialog::getText(this,tr("Add Stye"),tr("Style Name"));
    if(sName.isEmpty())
        return;

    StyleItem * pStyleItem = TaskManager::getInstance()->addStyle(sName);
    if(pStyleItem == NULL)
        return;

    ui->tableWidget->insertRow(ui->tableWidget->rowCount() - 1);
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,0,new QTableWidgetItem(sName));
    QTableWidgetItem* pTableItem = new QTableWidgetItem("150-150-150");
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,1,pTableItem);

    ui->tableWidget->setCurrentItem(pTableItem);

    on_pushButton_EditStyle_clicked();
}

void TaskStyleEditor::on_pushButton_RemoveStyle_clicked()
{
    // 突然觉得，这个添加和删除按钮，没有太大的意义
}
